#include "registerwindow.h"
#include "ui_registerwindow.h"

registerWindow::registerWindow(void(*func)(void *),void *obj,QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::registerWindow)
{
    ui->setupUi(this);

    db = DBOperation::getInstance();
    db->open();

    lp = func;
    lpobj = obj;

    ui->registerPushButton->setStyleSheet(
                "QPushButton{"
                                                 "color: rgb(0, 0, 0);"
                                                 "border:2px groove gray;"
                                                 "border-color: rgb(0, 0, 0);"
                                                 "border-radius:10px;"
                                                 "background-color: rgba(20,196,188, 210);"
                                                 "font:36px '宋体';}"
                                   "QPushButton::hover{"
                                                 "border:2px groove gray;"
                                                 "border-color: rgb(255, 255, 255);"
                                                 "background-color: rgba(251,102,102, 200);"
                                                 "color:rgb(255,255,255);}"
                                    "QPushButton::pressed{"
                                                 "color: rgb(255, 0, 0);"
                                                 "border:2px groove gray;"
                                                 "border-color: rgb(255, 0, 0);"
                                                 "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(20,196,188, 210), stop:1 rgba(251,102,102, 200));}");



}

registerWindow::~registerWindow()
{
    db->close();
    delete ui;
}


void registerWindow::on_registerPushButton_clicked()
{
    QString username = ui->usernameLineEdit->text();
    QString password = ui->passwordLineEdit->text();
    QString realName = ui->realNameLineEdit->text();
    QString sex = ui->sexComboBox->currentText();
    QString birthday = ui->birthdayDateEdit->text();
    QString address = ui->addressLineEdit->text();
    QString tel = ui->telLineEdit->text();
    QString email = ui->emailLineEdit->text();
//    QString registerDate = ui->registerDateLineEdit-> 注册时间取按下注册按钮的那一刻
    QString readNumber = ui->readerNumberLineEdit->text();
    QString sql;

    if (username.isEmpty() || password.isEmpty() || readNumber.isEmpty()) {
        QMessageBox::warning(this, "警告", "请输入必填内容");
    }
    else {
        sql = "select * from reader_info;";
        db->operate(sql);
        bool flag = true;
        while (db->getNext()) {
            if (db->getValue("username") == username) {
                flag = false;
                QMessageBox::warning(this, "提示", "该用户名已存在");
                break;
            }
        }
        if (flag) {
            QDateTime registerDate = QDateTime::currentDateTime();

            sql = QString("INSERT INTO reader_info VALUES('%0', '%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8', '%9');").arg(username, password, realName, sex, birthday, address, tel, email, registerDate.toString("yyyy-MM-dd hh:mm:ss"), readNumber);
            qDebug() << sql;
            db->operate(sql);
            QMessageBox::information(this, "提示", "注册成功");
            on_cancelPushButton_clicked();
        }
    }
}


void registerWindow::on_cancelPushButton_clicked()
{
    this->lp(lpobj);
    this->close();
}

